package com.zjdiepu.www.task.thhandleerr;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ThirdModeIDType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.ibiz.partner.IThHandleErrService;
import com.zjdiepu.www.ibiz.partner.IThLendingService;
import com.zjdiepu.www.ibiz.partner.IThYwBusinessRequestService;
import com.zjdiepu.www.ibiz.partner.IThYwBusinessService;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.ibiz.zpp.XinHeHuiApiService;
import com.zjdiepu.www.model.partner.dto.ThHandleErr;
import com.zjdiepu.www.model.partner.ext.ThHandleErrExt;
import com.zjdiepu.www.model.system.dto.BaseSysParams;
@Component
public class ThHandleErrTask {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private IThHandleErrService thHandleErrService;
	@Resource
	private IBaseSysParamsService baseSysParamsService;
	@Resource
	private XinHeHuiApiService xinHeHuiApiService;
	
	@Resource
	private IThYwBusinessService thYwBusinessService;
	@Resource
	private IThYwBusinessRequestService thYwBusinessRequestService;
	@Resource
	private IThLendingService thLendingService;
	
	@Scheduled(cron="${schedule.thhandlererr.alert.cron}")
	public  void thHandleErr() {
//		BaseSysParams sys1= new BaseSysParams();
//		sys1.setCodeParam("Interval_Retry_Count");
//		sys1=baseSysParamsService.selectRefined(sys1);
		BaseSysParams retryCountParam= new BaseSysParams();
		retryCountParam.setDpStatus(DpStatus.NORMAL.getCode());
		retryCountParam.setCodeParam("Retry_Count");
		retryCountParam=baseSysParamsService.selectRefined(retryCountParam);
		int retryCount = 0;
		if(retryCountParam!=null){
			String valueParam = retryCountParam.getValueParam();
			try {
				retryCount=Integer.parseInt(valueParam);
			} catch(NumberFormatException e) {
				logger.error("解析重试次数失败,参数为：{}.", valueParam, e);
			}
		}
		ThHandleErrExt thHandleErrExt=new ThHandleErrExt();
		thHandleErrExt.setDpStatus(DpStatus.NORMAL.getCode());
		thHandleErrExt.setIsFinish(0);
		int rows = 50;
		thHandleErrExt.setRows(rows);
		int count = thHandleErrService.countByExt(thHandleErrExt);
		int pages = count % rows == 0 ? count / rows : count / rows + 1;
		for(int page = 1; page <= pages; page++) {
			thHandleErrExt.setPage(page);
			List<ThHandleErr> thHandleErrs=thHandleErrService.selectByExt(thHandleErrExt);
			if(CollectionUtils.isEmpty(thHandleErrs)){
				break;
			}
			for (ThHandleErr thHandleErr : thHandleErrs) {
				String thirdModelId = thHandleErr.getThirdModeId();
				if(StringUtils.isNotBlank(thirdModelId)){
					String busiId = thHandleErr.getBusiId();
					String busiType = "xhh";
					String sFlag = "fail";
					boolean flag=false;
					
					if(ThirdModeIDType.TJCS.getCode().equals(thirdModelId) ) {
						flag = thYwBusinessRequestService.saveApplyXinHeHuiLoan(busiId, ThirdModeIDType.TJCS.getCode(), ThirdModeIDType.TJCS.getValue());
					} else if(ThirdModeIDType.CSCX.getCode().equals(thirdModelId)) {
						flag = thYwBusinessRequestService.saveQueryXinHeHuiStatus(busiId, 1, ThirdModeIDType.CSCX.getCode(), ThirdModeIDType.CSCX.getValue());
					} else if(ThirdModeIDType.TJZS.getCode().equals(thirdModelId)) {
						flag = thYwBusinessRequestService.saveSubmitXinHeHuiAudit(busiId, ThirdModeIDType.TJZS.getCode(), ThirdModeIDType.TJZS.getValue());
					} else if(ThirdModeIDType.ZHCX.getCode().equals(thirdModelId)) {
						flag = thYwBusinessRequestService.saveQueryXinHeHuiStatus(busiId, 2, ThirdModeIDType.ZHCX.getCode(), ThirdModeIDType.ZHCX.getValue());
					} else if(ThirdModeIDType.FXSPCX.getCode().equals(thirdModelId)) {
						flag = thYwBusinessRequestService.saveQueryXinHeHuiStatus(busiId, 3, ThirdModeIDType.FXSPCX.getCode(), ThirdModeIDType.FXSPCX.getValue());
					} else if(ThirdModeIDType.FKCX.getCode().equals(thirdModelId)) {
						flag = thLendingService.saveApplyXinHeHuiLending(busiId, ThirdModeIDType.FKCX.getCode(), ThirdModeIDType.FKCX.getValue());
					}else if(ThirdModeIDType.MJWCTZ.getCode().equals(thirdModelId)) {
						sFlag=xinHeHuiApiService.diepuMuJiOrJihuaApi(ThirdModeIDType.MJWCTZ.getCode(), ThirdModeIDType.MJWCTZ.getValue(), busiId, busiType);
					}else if(ThirdModeIDType.DCZTCX.getCode().equals(thirdModelId)) {
						flag = thYwBusinessRequestService.saveQueryRepay(busiId, ThirdModeIDType.DCZTCX.getCode(), ThirdModeIDType.DCZTCX.getValue());
					}
					
					if(flag || "success".equals(sFlag)){
						thHandleErr.setIsFinish(1);
					} else {
						int icount = 0;
						Integer countRetry = thHandleErr.getCountRetry();
						if(countRetry !=null ){
							icount = countRetry + 1;
						} else {
							icount = 1;
						}
						thHandleErr.setCountRetry(icount);
						if(icount >= retryCount) {
							thHandleErr.setIsFinish(1);
							thHandleErr.setRemarks("重试次数超过了配置的【" + retryCount + "】。");
						}
					}
					thHandleErr.initUpdateData(SystemConsts.DEFAULT_ID, "定时任务重新发送请求");
					thHandleErrService.saveUpdateAndDelete(thHandleErr, OperateType.edit);
				}
			}
		}
	}

}
